#include<stdio.h>
#include<malloc.h>
typedef int Elemtype
typedef struct LNode{
	Elemtype data;
	struct LNode *next;
}LinkNode;
void CreateList(LinkNode *L,Elemtype a[],int n)
{
	LinkNode *p;
	p = (LinkNode *)malloc(sizeof(LinkNode));
	p->next == NULL;
	for(int i = 0;i < n;i++)
	{
		s = (LinkNode *)malloc(sizeof(LinkNode));
		s->data = a[i];
		s->next =L->next;
		L->next = s;
	}
}
void Initialist(LinkNode *L)
{
	L=(LinkNode *)malloc(sizeof(LinkNode));
	L->next = NULL;
}
void DestroyList(LinkNode *L)
{
	LinkNode *pre = L,*p = pre->next;
	while(p!=NULL)
	{
		free(pre);
		pre = p;
		p = pre->next;
	}
	free(pre);
}
bool ListEmpty(LinkNode *L)
{
	return(L->next==NULL);
}
int Listlength(LinkNode *L)
{
	int i = 0;
	LinkNode *p=L;
	while(p->next!=NULL)
	{
		i++;
		p = p->next;
	}
	return(i);
}
void DispList(LinkNode *L)
{
	LinkNode *p = L->next;
	while(p!=NULL)
	{
		printf("%d",p->data);
		p = p->next;
	}
	printf("\n");
 }
 bool GetElem(LinkNode *L,int i,Elemtype &e)
 {
 	int j = 0;
 	LinkNode *p = L;
 	if(i<0)
 	return false;
 	while(p!=NULL&&j<0)
 	{
 		j++;
 		p = p->next;
	 }
	 if(p==NULL)
	 return false;
	 else
	 e=p->data;
	 return true;
 }
 int LocateElem(LinkNode *L,Elemtype e)
 {
 	int i = 1;
 	LinkNode *p = L->next;
	 while(p!=NULL&&p->data!=e)
	 {
	 	p = p->next;
		 i++; 
	  } 
	  if(p==NULL)
	  {
	  	return(0);
	  }
	  else
	  return(i);
 }
 bool ListInsert(LinkNode *L,int i,Elemtype e)
 {
 	int j = 0;
 	LinkNode *p = L,*s;
 	if(i<0)
 	return false;
 	while(p!=NULL&&j<i-1)
 	{
 		j++;
 		p = p->next;
	 }
	 if(p==NULL)
	 return false;
	 else{
	 	s = (LinkNode *)malloc(sizeof(LinkNode));
	 	s->data = e;
	 	s->next = p->next;
	 	p->next = s;
	 	return true;
	 }
 }
 bool ListDelete(LinkNode *L,int i,Elemtype &e)
 {
 	LinkNode *s = L,*q;
 	int j = 0;
 	if(i<0)
 	return false;
 	while(j<i-1&&s!=NULL)
 	{
 		j++;
 		s = s->next;
	 }
	 if(s==NULL)
	 return false;
	 else
	 {
	 	q=s->next;
	 	if(q==NULL)
	 	return false;
	 	e = s->data;
	 	s->next = q->next;
	 	free(q);
	 	return true;
	 }
 }
 
 